<?php

namespace App\Admin\Components;

use App\Models\Owner;
use App\Models\OwnerItem;
use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class HouseTickets extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('收租情况');
        $this->height(400);
        $this->chartHeight(300);
        $this->chartLabels('已收 / 应收');
        $this->dropdown([
            '7' => '最近七天',
            '28' => '最近28天',
            '30' => '最近30天',
            '365' => '最近一年',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '365':
                $this->getData(365);
                break;
            case '30':
                $this->getData(30);
                break;
            case '28':
                $this->getData(28);
                break;
            case '7':
                $this->getData(7);
                break;
            default:
                $this->getData(7);
                break;
        }
    }


    public function getData($days)
    {
        // 计算租客数量
        $ownerCount = Owner::query()->count();
        // 计算本月应收
        $ownerItem = OwnerItem::query()
            ->where('started_at', ">", Carbon::now()->subDays($days))
            ->where("started_at", "<", Carbon::now())
            ->get();

        $expectedIncome = 0; // 预期收入
        $actualIncome = 0;   // 实际收入
        $unpaidOwner = 0;    // 未缴费用户
        $paidOwner = 0;      // 已缴费用户

        foreach ($ownerItem as $item) {
            if ($item->total_amount) {   // 计算预期收入
                $expectedIncome += doubleval($item->total_amount);
            }

            if ($item->total_amount && $item->pay_at) {
                $actualIncome += doubleval($item->total_amount);
                $paidOwner ++;
            } else {
                $unpaidOwner ++;
            }
        }
        // 租客数量
        $this->withContent($ownerCount);
        // 卡片底部
        $this->withFooter($expectedIncome, $actualIncome, $unpaidOwner, $paidOwner);
        if ($expectedIncome === 0) {
            $proportion = 100;
        } else {
            $proportion = (int)($actualIncome * 100 / $expectedIncome) ;
        }
        // 图表数据
        $this->withChart($proportion);
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart(int $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center">
    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
    <small>我的租客总数</small>
</div>
HTML
        );
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response, $paidOwner)
    {
        $marginRight = 5;
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>应收</p>
        <span class="font-lg-1">{$new}</span>
    </div>
    <div class="text-center">
        <p>已收</p>
        <span class="font-lg-1">{$open}</span>
    </div>
    <div class="text-center">
        <p>未交租数量</p>
        <span class="font-lg-1">{$response}</span>
    </div>
    <div class="text-center">
        <p>已交租数量</p>
        <span class="font-lg-1">{$paidOwner}</span>
    </div>
</div>
HTML
        );
    }
}
